// pages/newsDetail/newsDetail.js
var api = require("../../conf/api")
var auth = require("../../conf/auth")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_avatar:null,
    news:{},
    isShowCommentModal:false,
    reply:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let newsId = options.newsId
    wx.request({
      url: api.newsURL + newsId + "/",
      data: {
        newsId
      },
      header:{
          Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      dataType: "json",
      method: "GET",
      responseType: "text",
      success: (res) => {
        that.setData({
          news:res.data,
          user_avatar:app.globalData.userInfo ? app.globalData.userInfo.avatarUrl:null
        })
        console.log(res.data)
      },
      fail: (err) => {},
    })
  },
  // 更多评论
  getMoreComment(e){
    let that = this

    let root_id = e.target.dataset.rootid
    let id=e.target.dataset.rootindex
    wx.request({
      url: api.CommentAPI,
      data: {
        root_id
      },
      dataType: 'json',
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
    },
      method: 'GET',
      responseType: 'text',
      success: (res) => {
        if(res.data.length<=1){
          wx.showToast({
            title: '没有更多评论了',
            icon:"none"
          })
          return
        }
        that.setData({
          ["news.comment["+ id +"].child"]:res.data
        })
        console.log(res.data)
      },
      fail: (err) => {},
    })
    
  },
  // 点击评论
  onClickShowCommentModal(e){
    if(!app.globalData.userInfo){
      wx.navigateTo({
        url: '/pages/local/local',
      })
      return
    }
    this.setData({
      isShowCommentModal:true,
      reply:e.target.dataset
    })
  },
  // 取消评论
  onClickCancelCommentModal(){
    this.setData({
      isShowCommentModal:false,
      reply:{}
    })
  },
  onClickClearReply(){
    this.setData({
      reply:{
        depth:1,
        news:this.data.reply.news
      }
    })
  },
  // 评论内容
  inputComment(e){
    this.setData({
      ['reply.content']:e.detail.value
    })
  },
  // 发布
  onClickPostComment(){
    let that = this
    if(!this.data.reply.content){
      wx.showToast({
        title: '评论不能为空',
        icon:"none"
      })
      return
    }
    wx.request({
      url: api.CommentAPI,
      data:{
        news:this.data.reply.news,
        depth:this.data.reply.depth,
        reply:this.data.reply.reply,
        content:this.data.reply.content,
        root:this.data.reply.root,
        token:app.globalData.userInfo.token
      },
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      method:'POST',
      dataType:'json',
      responseType:'text',
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      success(res){
        if(res.statusCode==201){
          wx.showLoading({
            title: '加载中',
          })
        if(that.data.reply.rootindex === undefined){
          var data_list = that.data.news.comment;
          data_list.unshift(res.data)
          setTimeout(function () {   
            wx.hideLoading()
            that.setData({
              ["news.comment"]:data_list,
              ["news.comment_count"]:that.data.news.comment_count+1
            })
        }, 500) 
          that.onClickCancelCommentModal()
        }else{
          console.log(that.data.reply.rootindex)
          let childCommentList = that.data.news.comment[that.data.reply.rootindex].child ? that.data.news.comment[that.data.reply.rootindex].child : []
          console.log(res.data)
          childCommentList.unshift(res.data);
          console.log(childCommentList)
          setTimeout(function () {   
            wx.hideLoading()
            that.setData({
              ["news.comment["+that.data.reply.rootindex+"].child"]:childCommentList,
              ["news.comment_count"]:that.data.news.comment_count+1
            })
            that.onClickCancelCommentModal()
        }, 500) 
          
        }
          
          
        }
      },
      fail(err){
        console.log(err)
      }
    })
  },
  // 回复的赞功能
  doCommentFavor(e){
    let that = this
    let result = auth.is_login()
    if(!result){
      wx.navigateTo({
        url: '/pages/local/local',
      })
      return
    }
    let cid =e.target.dataset.cid
    let rindex = e.target.dataset.rootindex
    let cindex=e.target.dataset.childindex
    wx.request({
      url: "http://127.0.0.1:8000/api/comment_favor/",
      data:{
        comment:cid
      },
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      method:"POST",
      dataType:"JSON",
      responseType:"text",
      success(res){
        if(res.statusCode===200){ 
          if(cindex===undefined){
            that.setData({
              ["news.comment["+rindex+"].favor"]:false
            })
          }else{    
            that.setData({
              ["news.comment["+rindex+"].child["+cindex+"].favor"]:false
            })
          }
          
          console.log("删除成功")

        }else if(res.statusCode===201){
          if(cindex===undefined){
            that.setData({
              ["news.comment["+rindex+"].favor"]:true
            })
          }else{
            that.setData({
              ["news.comment["+rindex+"].child["+cindex+"].favor"]:true
            })
          }
          
          console.log("点赞成功")
        }else{
          console.log(res)
          wx.showToast({
            title: "请求错误",
            icon:"none"
          })
          return
        }
      },
      fail(err){
        console.log(err)
      }
    })
    console.log(e.target.dataset)
  },
  doNewsFavor(e){
    let that = this
    let result = auth.is_login()
    if(!result){
      wx.navigateTo({
        url: '/pages/local/local',
      })
      return
    }
    
    // 访问的详情页id
    let newsId =e.target.dataset.nid
    wx.request({
      url: api.FavorURL,
      data:{
        news:newsId
      },
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      method:"POST",
      dataType:"json",
      responseType:"text",
      success(res){
        if(res.statusCode===200){
          console.log("删除成功")
          that.setData({
            ["news.favor"]:false,
            ["news.favor_count"]:that.data.news.favor_count-1
          })
        }else if(res.statusCode===201){
          that.setData({
            ["news.favor"]:true,
            ["news.favor_count"]:that.data.news.favor_count+1
          })
          console.log("点赞成功")
        }else{
          wx.showToast({
            title: "请求错误",
            icon:"none"
          })
          return
        }
      },
      fail(err){
        console.log(err)
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})